/* util */
import { isFunction } from '@src/util/type'
/* constants */
const MethodNames = [
  'abort',
  'append',
  'blur', 
  'change', 
  'close',
  'clear', 
  'clearCheckedNodes',
  'clearQuery',
  'clearValidate',
  'clearSelection',
  'clearFiles',
  'clearSort',
  'clearFilter',
  'closeDrawer',
  'doLayout',
  'expand-change',
  'focus',
  'filter',
  'getCheckedNodes',
  'getCheckedKeys',
  'getHalfCheckedNodes',
  'getHalfCheckedKeys',
  'getCurrentKey',
  'getCurrentNode',
  'getNode',
  'input',
  'insertBefore',
  'insertAfter',
  'next',
  'open',
  'prev',
  'remove',
  'remove-tag', 
  'resetField',
  'resetFields',
  'select',
  'setActiveItem',
  'submit',
  'setCurrentRow',
  'setCheckedNodes',
  'setCheckedKeys',
  'setChecked',
  'setCurrentKey',
  'setCurrentNode',
  'sort',
  'toggleRowSelection',
  'toggleAllSelection',
  'toggleRowExpansion',
  'updateKeyChildren',
  'visible-change', 
  'validate',
  'validateField',
]

export default {
  name: 'el-component-event-mixin',
  methods: {
    /**
     * @description: 绑定方法
    */    
    bindElementMethods() {
      // element元素
      const elementElement = this.$refs.PublinkElement
      if (!elementElement) {
        return console.warn(`Caused: ${this.name} refs.PublinkElement is empty`)
      }
      
      let isBind = false
      // 遍历element元素
      for (let property in elementElement) {
        /** 
         * 是否绑定
         * 1. 存在此属性
         * 2. 此属性类型为方法
         * 3. 是指定的绑定事件名称列表中的事件
        */
        isBind = (
          elementElement.hasOwnProperty(property) 
          && isFunction(elementElement[property])
          && MethodNames.indexOf(property) > -1
        )
        
        if (!isBind) continue
        
        // 绑定事件
        this[property] = (...args) => {
          console.info(`el-component ${property} method is called`)
          return elementElement[property] && elementElement[property](...args)
        }
      }
    }
  },
  mounted() {
    this.bindElementMethods()
  }
}